/**
 * 
 */
package com.soap;

import java.io.IOException;
import java.net.SocketTimeoutException;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

import com.clases.Categoria;
import com.clases.Lugar;

/**
 * @author Chris
 */
public class Soap_Lugar {

	private static String TAG = "SOAP LUGAR";

	SoapSerializationEnvelope envelope;

	PropertyInfo pi;

	SoapObject Request;

	private Lugar[] getInformacion(Object soap) {

		String RES = soap.toString();
		try {
			JSONArray jsonArray = new JSONArray(RES);
			Lugar[] lugs = new Lugar[jsonArray.length()];
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jo = new JSONObject(jsonArray.getString(i));
				int IDLugar = Integer.parseInt(jo.getString("IDLugar"));
				String NombreLugar = jo.getString("Nombre");
				String Direccion = jo.getString("Direccion");
				String Descripcion = jo.getString("Descripcion");
				String Portada = jo.getString("Portada");
				int Puntaje = jo.getInt("Puntaje");
				// CATEGORIA
				String categoria = jsonArray.getJSONObject(i)
						.getJSONObject("Categoria").toString();
				jo = new JSONObject(categoria);
				int IDCategoria = jo.getInt("IDCategoria");
				String NombreCategoria = jo.getString("NombreCategoria");

				// CATEGORIA
				Categoria cat = new Categoria();
				cat.setIDCategoria(IDCategoria);
				cat.setNombreCategoria(NombreCategoria);

				// LLUGAR
				Lugar lg = new Lugar();
				lg.setCategoria(cat);
				lg.setDescripcion(Descripcion);
				lg.setDireccion(Direccion);
				lg.setIDLugar(IDLugar);
				lg.setNombre(NombreLugar);
				lg.setPortada(Portada);
				lg.setPuntaje(Puntaje);

				lugs[i] = lg;

			}
			return lugs;
		}
		catch (Exception x) {

			Lugar[] lugs = new Lugar[1];
			Lugar lk = new Lugar();
			lk.setNombre(x.getMessage());
			lugs[0] = lk;

			return lugs;
		}
	}

	public Lugar[] getLugaresBusqueda(String lugar, int last) {

		String METHOD_NAME = "getLugaresBusqueda";
		String SOAP_ACTION = SoapUtil.NAMESPACE + METHOD_NAME;

		Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);

		Request.addProperty("nombre", lugar);
		Request.addProperty("last", last);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(Request);
		HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL,
				SoapUtil.TIMEOUT);

		Object response = null;

		try {

			httpTransport.call(SOAP_ACTION, envelope);
			response = envelope.getResponse();

			return getInformacion(response);

		}
		catch (SocketTimeoutException x) {
			Log.e(TAG + ": ", x.getMessage());

			Lugar[] lugs = new Lugar[1];
			Lugar lk = new Lugar();
			lk.setIDLugar(-10);
			lugs[0] = lk;
			return lugs;
		}
		catch (SoapFault e) {
			StackTraceElement[] stack = e.getStackTrace();
			String x = " ";
			for (StackTraceElement line : stack) {
				x += line.toString();
			}
			Log.e(TAG + ": ", x);

			Lugar[] lugs = new Lugar[1];
			Lugar lk = new Lugar();
			lk.setNombre(x);
			lugs[0] = lk;
			return lugs;

		}
		catch (IOException x) {
			Log.e(TAG + ": ", x.getMessage());

			Lugar[] lugs = new Lugar[1];
			Lugar lk = new Lugar();
			lk.setNombre(x.getMessage());
			lugs[0] = lk;
			return lugs;
		}
		catch (XmlPullParserException x) {
			Log.e(TAG + ": ", x.getMessage());

			Lugar[] lugs = new Lugar[1];
			Lugar lk = new Lugar();
			lk.setNombre(x.getMessage());
			lugs[0] = lk;
			return lugs;
		}

	}

	public Lugar[] getLugaresCategoria(int categoria, int last) {

		String METHOD_NAME = "getLugaresCategoria";
		String SOAP_ACTION = SoapUtil.NAMESPACE + METHOD_NAME;

		Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);

		Request.addProperty("idCategoria", categoria);
		Request.addProperty("last", last);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(Request);
		HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL,
				SoapUtil.TIMEOUT);

		Object response = null;

		try {

			httpTransport.call(SOAP_ACTION, envelope);
			response = envelope.getResponse();

			return getInformacion(response);

		}
		catch (SocketTimeoutException x) {
			Log.e(TAG + ": ", x.getMessage());

			Lugar[] lugs = new Lugar[1];
			Lugar lk = new Lugar();
			lk.setIDLugar(-10);
			lugs[0] = lk;
			return lugs;
		}
		catch (SoapFault e) {
			StackTraceElement[] stack = e.getStackTrace();
			String x = " ";
			for (StackTraceElement line : stack) {
				x += line.toString();
			}
			Log.e(TAG + ": ", x);

			Lugar[] lugs = new Lugar[1];
			Lugar lk = new Lugar();
			lk.setNombre(x);
			lugs[0] = lk;
			return lugs;

		}
		catch (IOException x) {
			Log.e(TAG + ": ", x.getMessage());

			Lugar[] lugs = new Lugar[1];
			Lugar lk = new Lugar();
			lk.setNombre(x.getMessage());
			lugs[0] = lk;
			return lugs;
		}
		catch (XmlPullParserException x) {
			Log.e(TAG + ": ", x.getMessage());

			Lugar[] lugs = new Lugar[1];
			Lugar lk = new Lugar();
			lk.setNombre(x.getMessage());
			lugs[0] = lk;
			return lugs;
		}

	}

	public Lugar[] getLugaresInicio(int last) {

		String METHOD_NAME = "getLugaresInicio";
		String SOAP_ACTION = SoapUtil.NAMESPACE + METHOD_NAME;

		Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);

		Request.addProperty("last", last);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(Request);
		HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL,
				SoapUtil.TIMEOUT);

		Object response = null;

		try {

			httpTransport.call(SOAP_ACTION, envelope);
			response = envelope.getResponse();

			return getInformacion(response);

		}
		catch (SocketTimeoutException x) {
			Log.e(TAG + ": ", x.getMessage());

			Lugar[] lugs = new Lugar[1];
			Lugar lk = new Lugar();
			lk.setIDLugar(-10);
			lugs[0] = lk;
			return lugs;
		}
		catch (SoapFault e) {
			StackTraceElement[] stack = e.getStackTrace();
			String x = " ";
			for (StackTraceElement line : stack) {
				x += line.toString();
			}
			Log.e(TAG + ": ", x);

			Lugar[] lugs = new Lugar[1];
			Lugar lk = new Lugar();
			lk.setNombre(x);
			lugs[0] = lk;
			return lugs;

		}
		catch (IOException x) {
			Log.e(TAG + ": ", x.getMessage());

			Lugar[] lugs = new Lugar[1];
			Lugar lk = new Lugar();
			lk.setNombre(x.getMessage());
			lugs[0] = lk;
			return lugs;
		}
		catch (XmlPullParserException x) {
			Log.e(TAG + ": ", x.getMessage());

			Lugar[] lugs = new Lugar[1];
			Lugar lk = new Lugar();
			lk.setNombre(x.getMessage());
			lugs[0] = lk;
			return lugs;
		}

	}

	public int getRateLugar(int idLugar) {
		String METHOD_NAME = "getevaluacion";
		String SOAP_ACTION = SoapUtil.NAMESPACE + METHOD_NAME;

		Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);

		Request.addProperty("id_lugar", idLugar);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(Request);
		HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL,
				SoapUtil.TIMEOUT);

		Object response = null;

		try {

			httpTransport.call(SOAP_ACTION, envelope);
			response = envelope.getResponse();

			Log.e("RESPONSE", response.toString());

		}
		catch (SocketTimeoutException x) {
			Log.e(TAG + ": ", x.getMessage());

		}
		catch (SoapFault e) {
			StackTraceElement[] stack = e.getStackTrace();
			String x = " ";
			for (StackTraceElement line : stack) {
				x += line.toString();
			}
			Log.e(TAG + ": ", x);

		}
		catch (IOException x) {
			Log.e(TAG + ": ", x.getMessage());

		}
		catch (XmlPullParserException x) {
			Log.e(TAG + ": ", x.getMessage());

		}

		return Integer.parseInt(response.toString());
	}

	public void rateLugar(int puntaje, int idlugar, int idusuario) {

		String METHOD_NAME = "Evaluar";
		String SOAP_ACTION = SoapUtil.NAMESPACE + METHOD_NAME;

		Request = new SoapObject(SoapUtil.NAMESPACE, METHOD_NAME);

		Request.addProperty("idLugar", idlugar);
		Request.addProperty("Puntaje", puntaje);
		Request.addProperty("IdUsuario", idusuario);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(Request);
		HttpTransportSE httpTransport = new HttpTransportSE(SoapUtil.URL,
				SoapUtil.TIMEOUT);

		Object response = null;

		try {

			httpTransport.call(SOAP_ACTION, envelope);
			response = envelope.getResponse();

			Log.e("RESPONSE", response.toString());

		}
		catch (SocketTimeoutException x) {
			Log.e(TAG + ": ", x.getMessage());

		}
		catch (SoapFault e) {
			StackTraceElement[] stack = e.getStackTrace();
			String x = " ";
			for (StackTraceElement line : stack) {
				x += line.toString();
			}
			Log.e(TAG + ": ", x);

		}
		catch (IOException x) {
			Log.e(TAG + ": ", x.getMessage());

		}
		catch (XmlPullParserException x) {
			Log.e(TAG + ": ", x.getMessage());

		}

	}
}
